#include "hittableList.h"
#include "constantMedium.h"

double HittableList::pdf_value(const Point3& origin, const Vec3& direction) const
{
    auto weight = 1.0 / objects.size();
    auto sum = 0.0;

    for (const auto& object : objects)
        sum += weight * object->pdf_value(origin, direction);

    return sum;
}

Vec3 HittableList::random(const Point3& origin) const
{
    auto int_size = int(objects.size());
    return objects[random_int(0, int_size - 1)]->random(origin);
}

bool HittableList::hit(const Ray& r, Interval ray_t, HitRecord& rec) const
{
    HitRecord temp_rec;
    bool hit_anything = false;
    auto closest_so_far = ray_t.max;

    for (const auto& object : objects)
    {
        if (object->hit(r, Interval(ray_t.min, closest_so_far), temp_rec))
        {
            hit_anything = true;
            closest_so_far = temp_rec.t;
                                rec = temp_rec;
        }
    }

    return hit_anything;
}